package com.hb.game.pk.ui.activity.mine;

import android.os.Bundle;
import android.text.method.KeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.Login;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.SpUtils;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class BindActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.btn_bind)
    Button btnBind;
    int status = 2;
    KeyListener keyListener;
    KeyListener keyListener1;
    String name;
    String zfb;
    MaterialDialog alertDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BindActivity.this.finish();
            }
        });
        keyListener1 = etName.getKeyListener();
        keyListener = etPhone.getKeyListener();
        Login.DataBean userInfo = APP.userInfo;
        if (userInfo.getZfb_num() != null && userInfo.getZfb_num() != null) {
            if (!StringUtils.isEmpty(userInfo.getName().toString())) {
                etName.setText(userInfo.getName().toString());
            }
            if (!StringUtils.isEmpty(userInfo.getZfb_num().toString())) {
                etPhone.setText(userInfo.getZfb_num().toString());
            }
//            if (StringUtils.isEmpty(userInfo.getName().toString()) || StringUtils.isEmpty(userInfo.getZfb_num().toString())) {
//                btnBind.setText("确认绑定");
//                status = 2;
//            }
            if (!StringUtils.isEmpty(userInfo.getName().toString()) && !StringUtils.isEmpty(userInfo.getZfb_num().toString())) {
                etPhone.setKeyListener(null);
                etName.setKeyListener(null);
            }
        }
//        else {
//            status = 2;
        btnBind.setText("绑定");
//        }
    }

    @OnClick(R.id.btn_bind)
    public void onViewClicked() {
//        if (status == 1) {
//            Call<NullBean> call = RetrofitCreateHelper.createApi(RedBag.class, RedBag.HOST).zhifubao();
//            call.enqueue(new Callback<NullBean>() {
//                @Override
//                public void onResponse(Call<NullBean> call, Response<NullBean> response) {
//                    NullBean body = response.body();
//                    if (body == null) {
//                        ToastUtils.showToast("请求错误");
//                        return;
//                    }
//
//                    if (body.getStatus() != 1) {
//                        ToastUtils.showToast(body.getInfo());
//                        return;
//                    }
//                    status = 2;
//                    APP.userInfo.setZfb_num(null);
//                    APP.userInfo.setName(null);
//                    etPhone.setText("");
//                    etName.setText("");
//                    btnBind.setText("确定绑定");
//                    SpUtils.putString(BindActivity.this, "userInfo", new Gson().toJson(APP.userInfo));
//                    etName.setKeyListener(keyListener1);
//                    etPhone.setKeyListener(keyListener);
//                    ToastUtils.showToast(body.getInfo());
//                }
//
//                @Override
//                public void onFailure(Call<NullBean> call, Throwable t) {
//                    ToastUtils.showToast("网络请求错误");
//                }
//            });
//        } else {
        if (alertDialog == null) {
            View inflate = View.inflate(this, R.layout.dialog_alert, null);
            TextView tvCansle = (TextView) inflate.findViewById(R.id.tv_cansle);
            TextView tvOk = (TextView) inflate.findViewById(R.id.tv_ok);
            tvCansle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                }
            });
            tvOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                    name = etName.getText().toString();
                    zfb = etPhone.getText().toString();
                    if (StringUtils.isEmpty(name)) {
                        ToastUtils.showToast("请输入真实姓名");
                        return;
                    }
                    if (StringUtils.isEmpty(zfb)) {
                        ToastUtils.showToast("请输入支付宝账号");
                        return;
                    }
                    Call<NullBean> call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).bindZFB(name, zfb);
                    call.enqueue(new Callback<NullBean>() {
                        @Override
                        public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                            NullBean body = response.body();
                            if (body == null) {
                                ToastUtils.showToast("请求错误");
                                return;
                            }

                            if (body.getStatus() != 1) {
                                ToastUtils.showToast(body.getInfo());
                                return;
                            }
//                    status = 1;
                            APP.userInfo.setZfb_num(name);
                            APP.userInfo.setName(zfb);
//                btnBind.setText("解除绑定");
                            etPhone.setKeyListener(null);
                            etName.setKeyListener(null);
                            SpUtils.putString(BindActivity.this, "userInfo", new Gson().toJson(APP.userInfo));
                            ToastUtils.showToast(body.getInfo());
                        }

                        @Override
                        public void onFailure(Call<NullBean> call, Throwable t) {
                            ToastUtils.showToast("网络请求错误");
                        }
                    });
                }
            });
            alertDialog = new MaterialDialog.Builder(this).customView(inflate, false).show();
        } else {
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            } else {
                alertDialog.show();
            }
        }

//        }
    }
}
